// https://www.luogu.com.cn/problem/P2015
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
using ll = long long;
const int inf = 0x3f3f3f3f;
#define rf(i, n) for (int i = 1; i <= (n); ++i)
int rad();  // read int
const int max_size = 5 + 100;
const int maxn = 5 + 100;

int n, m;
struct Node {
    int to, w;
};

vector<Node> adj[maxn];
int f[maxn][maxn];

/*
1
3     4
2 5
*/
void dfs(int now, int fa, int maxs) {
    for (auto &p : adj[now]) {
        if (p.to == fa) continue;

        dfs(p.to, now, maxs - 1);
        for (int cnt = maxs; cnt >= 1; --cnt) {
            for (int k = 0; k < cnt; ++k)
                f[now][cnt] = max(f[now][cnt], f[now][cnt - k - 1] + f[p.to][k] + p.w);
        }
    }
}

int main() {
    n = rad(), m = rad();
    rf(i, n - 1) {
        int l = rad(), r = rad(), w = rad();
        adj[l].push_back({r, w}), adj[r].push_back({l, w});
    }
    dfs(1, 0, m);
    // for (int i = 1; i <= n; ++i) {
    //     for (int j = 1; j <= m; ++j)
    //         printf("%d ", f[i][j]);
    //     puts("");
    // }
    printf("%d", f[1][m]);
}

int rad() {
    int back = 0, ch = 0, neg = 0;
    for (; ch < '0' || ch > '9'; ch = getchar())
        neg = ch == '-';
    for (; ch >= '0' && ch <= '9'; ch = getchar())
        back = (back << 1) + (back << 3) + (ch & 0xf);
    return (back ^ -neg) + neg;
}